<?php
	if(!isset($_SESSION)){
		session_start();
	}

	// Clear previous messages.
	if(isset($_SESSION['error'])) session_unregister('error');
	if(isset($_SESSION['success'])) session_unregister('success');
	//if(isset($_SESSION['flag'])) session_unregister('flagcat');

	// Get values from the form.
	$categoryName = $_POST['categoryName'];
	$categoryDescription = $_POST['categoryDescription'];
	
	// Check for validation.
	if($categoryName == ""){
		$_SESSION['error'] = "Pleace complete the mandatory fields";
	}else{	
		include("../dbconnection.php");
		if($result = mysql_query("SELECT * FROM categories WHERE categoryName='$categoryName'")){
			if(mysql_num_rows($result) > 0){
				$row = mysql_fetch_assoc($result);
				$_SESSION['error'] = "Category name ".$row['categoryName']." is already exisist.";
			}else{
				if(mysql_query("INSERT INTO categories (categoryName, categoryDescription) VALUES ('$categoryName','$categoryDescription')")){
					$_SESSION['success'] = "Category added successfully.";
				}else{
					$_SESSION['error'] = "Error occured!";
				}
			}
		}else{
			$_SESSION['error'] = "Error occured!";
		}
	}
	//$_SESSION['flag']="cat";
	$_SESSION['page'] = "addCategories.php";
	header('Location: ../../');
?>
